﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleAppTestListAndIList
{
    class Program
    {
        static void Main(string[] args)
        {
            var employees1 = new Employees1
            {
                new Employee{Name="Mike"},
                new Employee{Name="Rose"}
            };
            IList<Employee> employees = employees1;
            employees.Add(new Employee { Name = "Steve" });//这一步走的还是ICollection里的Add方法，没有走自定义里的Add方法，所以报错
            foreach(var item in employees1)
            {
                Console.WriteLine(item.Name);
            }

            var employees2 = new Employees2
            {
                new Employee{Name="Mike"},
                new Employee{Name="Rose"}
            };
            ICollection<Employee> employeeslist2 = employees2;
            employeeslist2.Add(new Employee { Name = "Steve" });
            foreach (var item in employees2)
            {
                Console.WriteLine(item.Name);
            }
            Console.ReadLine();
        }
    }
    class Employee
    {
        public string Name { get; set; }
    }

    class Employees1:List<Employee>
    {
        public new void Add(Employee item)
        {
            item.Name += " Changed!";
            base.Add(item);
        }
    }
    class Employees2 : IEnumerable<Employee>,ICollection<Employee>
    {
        List<Employee> items = new List<Employee>();

        public int Count => throw new NotImplementedException();

        public bool IsReadOnly => throw new NotImplementedException();

        public void Add(Employee item)
        {
            item.Name += " Changed!";
            items.Add(item);
        }

        public  void Clear()
        {
            throw new NotImplementedException();
        }

        public bool Contains(Employee item)
        {
            throw new NotImplementedException();
        }

        public void CopyTo(Employee[] array, int arrayIndex)
        {
            throw new NotImplementedException();
        }

        public IEnumerator<Employee> GetEnumerator()
        {
           return items.GetEnumerator();
        }

        public bool Remove(Employee item)
        {
            throw new NotImplementedException();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }
    }
}
